<script>
	import store from '@/store/store.js'
	import {$api} from '@/config/api.js';
	export default {
		globalData: {
			$i18n: {},
			$t: {},
			store
		},
		onLaunch() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation("portrait-primary")
			if (this.$store.state.platform =='android') {
				// plus.navigator.setFullscreen(true);
				
				plus.navigator.hideSystemNavigation();
			}
			// #endif
			
			this.globalData.$i18n = this.$i18n
			this.globalData.$t = str => this.$t(str)
			// 读取client_id
			this.channelIDSyncFunc()
			uni.onNetworkStatusChange((res) => {
				let {
					isConnected
				} = res
				if (isConnected) {
					console.log('"?????????????????');
					this.initIndex()
					return
				}
			})
			
			this.initIndex()
		},
		methods: {
			async initIndex() {
				// 数据初始化
				this.dataInit();
				//自动登录
				this.autoLogin();
				// 读取设备信息
				this.getEquipmentCode();
				//下载
				// #ifdef APP-PLUS
				this.setDownLoad()
				// #endif
				// 获取用户定制信息
				this.userFormat();
				// setTimeout(() => {
				// 	// plus.navigator.closeSplashscreen();
				// }, 100)
				//设置初始化
				this.getSetting();
				//获取多游信息
				this.getDyInfo();
				//获取app开关配置列表
				this.getSwitchList();
				//禁止手机横屏操作
				// #ifdef APP-PLUS
					plus.screen.lockOrientation("portrait-primary")
				// #endif
				// this.hideTabBarItem();
				// this.setTabBarMidTap();
			},
			// 页面初始化
			dataInit() {
				this.$api.get("system/appinit").then(res => {
					console.log('页面初始化',res);
					uni.setStorageSync('isShowShopping', res.data.data.shop_switch)
					this.$store.commit("setInit", res.data.data);
					this.$store.commit("setDomain", res.data.data.domain);
				})
			},
			// 获取客户信息
			userFormat() {
				this.$api.get(`system/box_front_setup`).then(res => {
					console.log('获取客户信息',res);
					let userFormat = res.data.data.front_setup
					userFormat.down_app_url = userFormat.down_app_url + this.$store.state.client_id;
					userFormat.discount_topic = res.data.data.discount_topic
					this.$store.commit('setUserFormat', userFormat);
				})
			},
			// 自动登录
			autoLogin() {
				this.$store.commit("setUserInfo", {});
				this.$store.commit("setShopInfo", {});
				if (uni.getStorageSync('mem-username') && uni.getStorageSync('mem-password')) {
					let username = uni.getStorageSync('mem-username')
					let password = uni.getStorageSync('mem-password')
					this.$api.get("user/login", {
						'mem-username': username,
						'mem-password': password,
						equipmentCode: this.equipmentCode
					}).then(res => {
						if (res.data.code == 200) {
							// 将登录获取的数据记录下来
							this.$store.commit("setLoginInfo", res.data.data)
							// 获取用户信息
							this.$common.getuserInfo();
							//商城登录
							this.shopLogin(res.data)
						}
					})
				} else if (uni.getStorageSync('mem-openid')) {
					this.$api.get("user/login", {
						"mem-openid": uni.getStorageSync('mem-openid'),
						"mem-username": uni.getStorageSync('mem-username'),
						"mem-oauth_type": uni.getStorageSync('mem-oauth_type'),
					}).then(res => {
						console.log(res,'mem-openidmem-openid')
						if (res.data.code == 200) {
							// 将登录获取的数据记录下来
							this.$store.commit("setLoginInfo", res.data.data)
							// 获取用户信息
							this.$common.getuserInfo();
							//商城登录
							this.shopLogin(res.data)
						}
					})


				} else if (uni.getStorageSync('sms-mobile')) {
					this.$api.get("v8/user/loginm", {
						"sms-mobile": uni.getStorageSync('sms-mobile'),
						"sms-phone": '1',
						"sms-type": '2',
					}).then(res => {
						console.log(res, 'sms-mobilesms-mobilesms-mobile')
						// 将登录获取的数据记录下来
						if (res.data.code == 200) {
							if (res.data.data.is_pwd) {
								uni.setStorageSync('mem-username', res.data.data.username)
								uni.setStorageSync('sms-mobile',uni.getStorageSync('sms-mobile'))
								this.$store.commit("setLoginInfo", res.data.data)
								// 获取用户信息
								this.$common.getuserInfo();
								//商城登录
								this.shopLogin(res.data)
							}
						}
					})
				} else {
					// 清空本地存储的登录数据
					this.$store.commit('setInit', {});
					this.$store.commit('setLoginInfo', {});
					this.$store.commit('setUserInfo', {});
					uni.setStorageSync('mem-openid', '')
					uni.setStorageSync('mem-username', '')
					uni.setStorageSync('mem-password', '')
					uni.setStorageSync('sms-mobile','')


				}

			},
			shopLogin(res){
				$api.post('wp-json/myapi/token',{
					token: res.data.user_token
				}).then(res=>{
					if (res.data.code == 200) {
						this.$store.commit('setShopInfo',res.data.data)
					}
				})
				.catch(error=>{
					console.error('error',error)
				})
			},
			// 获取app开关配置列表
			getSwitchList(){
				$api.get('/app/switch/list',{
					type:0,
					topic_parent_id:1,
					button_version:1
				}).then(res => {
				   console.log('开关配置消息',res);
					if(res.data.code == 200){
					this.$store.commit('setSwitchList',res.data.data)
					}
				})
				.catch(error=>{
				   console.error('error',error)
				})
			 },
			//设置初始化
			getSetting() {
				let isMessage = uni.getStorageSync('isMessage')
				let isWifi = uni.getStorageSync('isWifi')
				let isWifiDown = uni.getStorageSync('isWifiDown')
				let isinstallGame = uni.getStorageSync('isinstallGame')
				let isClearPackage = uni.getStorageSync('isClearPackage')
				
				
				uni.setStorageSync('active',0);
				
				if (!isMessage) {
					uni.setStorageSync('isMessage', {
						type: 1,
						status: true
					})
				}
				if (!isWifi) {
					uni.setStorageSync('isWifi',{
						type: 1,
						status: true
					})
				}
				if (!isWifiDown) {
					uni.setStorageSync('isWifiDown',{
						type: 1,
						status: true
					})
				}
				if (!isinstallGame) {
					uni.setStorageSync('isinstallGame',{
						type: 1,
						status: true
					})
				}
				if (!isClearPackage) {
					uni.setStorageSync('isClearPackage',{
						type: 1,
						status: true
					})
				}
				
	
				
			},
			// 获取多游信息
			getDyInfo(){
				this.$api.get("duoyou/account").then(res => {
					console.log(res, 'res11111111')
					// 将登录获取的数据记录下来
					if (res.data.code == 200) {
						this.$store.commit("setDyInfo", res.data.data)
					}
				})
				.catch(error=>{
					console.log('error',error);
				})
			},
			// 读取设备信息
			getEquipmentCode() {
				// #ifdef APP-PLUS
				let that = this;
				plus.device.getInfo({
					success: function(e) {
						that.$store.commit("setEquipmentCode", e.uuid);
					},
					fail: function(e) {
						console.log('读取失败',e);
					}
				});
				// #endif
			},

			//下载管理 
			/* 设置下载管理状态 */
			setDownLoad() {
				// plus.downloader.clear(-1);
				let that = this
				plus.downloader.enumerate(function(tasks){
					// console.log("枚举下载数量: " + tasks.length, tasks);
					let downList = [];
					if (tasks.length) {
						tasks.forEach((task) => {
							if (Object.keys(task.options).length != 0){
								if (task.options.data === undefined) {
									task.abort();
								} else {
									let downTaskItem = JSON.parse(task.options.data);
									if (task.state == 3) {
										// console.log('重启app默认设置downstatus为暂停', task, downTaskItem)
										task.state = 5
										downTaskItem.status = 5
										task.pause()
									} else {
										downTaskItem.status = task.state
										if (task.state == 4) {
											downTaskItem.filename = task.filename
										}
									}
									// console.log('task', task)
									downList.unshift(downTaskItem);
								}
							}else{
								task.abort();
							}
						})
					}
					// console.log('目前枚举下载列表信息', downList)
					that.$store.commit('setDownTasksList', downList)
				},-1);
			},
			// 获取channelId
			channelIDSyncFunc() {
				let ret;         
				let udid;
				switch (uni.getSystemInfoSync().platform) {
					case "android":
						let getXWID = uni.requireNativePlugin("GetXWID")
						ret = getXWID.channelIDSyncFunc();
						console.log(ret,'retretretretrets')
						if (ret.channelid != 0) {
							this.$store.commit('setClientId', ret.channelid);
						} else {
							this.$store.commit('setClientId', 2);
						}
						break;
					case "ios":
						let channelModule = uni.requireNativePlugin("DCChannelUniPlugin-ChannelModule");
						ret = channelModule.channelIdSyncFunc({
							'name': 'uni-app',
							'age': 1
						}) || 2
						this.$store.commit('setClientId', ret);
						udid = channelModule.udidSyncFunc({
							'name': 'uni-app',
							'age': 1
						}) || ''
						this.$store.commit('setUdid', udid);       
						console.log(udid,'udidudidudid')
						break;
					default:
						break;
				}
			},
			hideTabBarItem(){
				uni.setTabBarItem({
					index: 2,//要隐藏的tabBar
					visible:false
				})
			},
			setTabBarMidTap() {
				uni.onTabBarMidButtonTap(()=>{
					console.log("点击了")
					uni.switchTab({
						url: '/pages/talkAbout/index'
					})
				})
			}
		},
		onShow(){
			// 获取用户设备id
			// #ifdef APP-PLUS
			this.$common.registerAsyncFunc()
			if (this.$store.state.platform =='android') {
				// plus.navigator.setFullscreen(true);
				plus.navigator.hideSystemNavigation();
			}
			// #endif
		}
	}

</script>

<style>
		/* #ifndef APP-PLUS-NVUE */
		/* @import "uview-ui/index.scss"; */
		/* @import './static/fonts/iconfont.css'; */
		/* #endif */
	 /* @import "./components/u-parse/u-parse.css" */
</style>
